package com.simba.server.controllers.impl;

import com.simba.server.components.CommandExecutor;
import org.apache.log4j.Logger;


import com.simba.common.log.LogUtil;

import com.simba.server.commands.impl.web.WebSlotStatusCmd;
import com.simba.server.components.SlotManager;
import com.simba.server.components.SortCommonDefines;
import com.simba.server.components.data.SlotInfo;
import com.simba.server.controllers.ISlotUnlockController;

public class SlotUnlockController implements ISlotUnlockController
{
	private static final Logger logger = Logger.getLogger(SlotUnlockController.class);

	@Override
	public void onSlotUnlockMessage(SlotInfo slotInfo)
	{
		try
		{
			/**
			 * 收容口不分配箱号
			 */
			Boolean res = SlotManager.getInstance().checkSlotIsTrash(slotInfo.getSlotId());
			if (res)
			{
				slotInfo.setLockStatus(SortCommonDefines.SLOT_SOFT_UNLOCK_FLAG);
				SlotManager.getInstance().setSlotSoftLockStatus(slotInfo);
				SlotManager.getInstance().sendSlotSoftUnLockToPLC(slotInfo);

				CommandExecutor.executorWebCommand(WebSlotStatusCmd.NAME, slotInfo,false);
				return;
			}

			/**
			 * 不上传的格口不分配箱号
			 */
			if (slotInfo.getSlotWhetherUpload() != 0)
			{
				slotInfo.setLockStatus(SortCommonDefines.SLOT_SOFT_UNLOCK_FLAG);
				SlotManager.getInstance().setSlotSoftLockStatus(slotInfo);
				SlotManager.getInstance().sendSlotSoftUnLockToPLC(slotInfo);

				CommandExecutor.executorWebCommand(WebSlotStatusCmd.NAME, slotInfo,false);
				return;
			}


			SlotManager.getInstance().doAllocBoxInfo(slotInfo);
		}
		catch (Exception ex)
		{
			LogUtil.LogDebug(SlotUnlockController.logger,
					"Handle NB Slot Unlock Message In NBSlotUnlockController Error -- Exception : "
							+ ex);
		}
	}
}
